<?php

namespace App\Models\Notice;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class Notice extends Model
{
    protected $table = 'notice';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    /**
     * 列表
     */
    public static function getList(Request $request, $memberinfo, $isgetall)
    {
        $manger_id = $memberinfo['id'] ?? 0;
        $cpid = $memberinfo['cpid'] ?? 0;
        $utype = $memberinfo['utype'] ?? 0;

        $query = self::orderBy('status', 'asc')->orderBy('create_at', 'desc');

        $start_time = $request->start_time;
        if ($start_time != '') {
            $start_time = strtotime(date('Y-m-d', $start_time) . ' 00:00:00');
            $query->where('create_at', '>=', $start_time);
        }

        $end_time = $request->end_time;
        if ($end_time != '') {
            $end_time = strtotime(date('Y-m-d', $end_time) . ' 23:59:59');
            $query->where('create_at', '<=', $end_time);
        }

        /***
         * 发送公告时，每个账号都会新插入一条公告，2025.04.22
        */
        if ($manger_id > 0) {
            $query->where('manger_id', '=', $manger_id);
        }

        if ($cpid > 0) {
            $query->where('cpid', $cpid);
        }
        if($utype > 0){
            $query->where('utype', $utype);
        }
        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }
}
